<?php
namespace Home\Controller;


class FoodController extends HomeController {
    //首页显示食材和口味
    public function index(){
        $food = M('food')->select();
        //dump($food);exit;
        $taste = M('taste')->select();
        $this->assign('food',$food);
        $this->assign('taste',$taste);
        $this->display();
    }
    //进行统计
    public function count(){
        if(empty($_POST)){
            $this->error("还没选呐");
        }
        $food = $_POST['food'];

        //设置统计值,方便事务,只提示一次成功
        $num = 0;

        //遍历食材,统计后对字段进行修改
        foreach ($food as $k => $v) {
            $data = M('food')->where(array('id'=>$v))->select();
            $choose = $data[0]['choose'] + 1;
            if(M('food')->where(array('id'=>$v))->setField('choose',$choose)>0){
                $num += 1;
            }
        }

        $taste = $_POST['taste'];

        //遍历食材,统计后对字段进行修改
        foreach ($taste as $key => $value) {
            $info = M('taste')->where(array('id'=>$value))->select();
            $chose = $info[0]['countnum'] + 1;
            if(M('taste')->where(array('id'=>$value))->setField('countnum',$chose)>0){
                $num += 1;
        }
    }
        //查询出综合数组条数,如果都成功了就提示成功
        $zonghe = count($food)+count($taste);

            if($num==$zonghe){
                $num=0;
                $this->success("OK啦~！",U('Food/index'));
            }else{
                $num = 0;
                $this->error("Sorry");
            }

    }
}